package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.constants.MessageConstant;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约服务实现类
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 添加预约
     * @param orderMap
     */
    @Override
    public Result addOrder(Map orderMap) throws Exception {
        // 获取电话
        String telephone = (String) orderMap.get("telephone");
        // 获取姓名
        String name = (String) orderMap.get("name");
        // 获取性别
        String sex = (String) orderMap.get("sex");
        // 获取身份证
        String idCard = (String) orderMap.get("idCard");
        // 获取预约日期
//        String setmealId = (String) orderMap.get("setmealId");
//        orderMap.put("setmeal_id",setmealId);

        String orderDate = (String) orderMap.get("orderDate");
        Date newOrderDate = DateUtils.parseString2Date(orderDate);
        // 获取套餐预约信息
        OrderSetting orderSetting  = orderSettingDao.queryOrderSetting(orderDate);
        // 查询当天是否可以预约
        if (orderSetting == null) {
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        // 查询套餐预约设置
        if (orderSetting.getNumber() <= orderSetting.getReservations()) {
            // 预约已满
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        // 用户是否新用户
        Member member  = memberDao.isMember(orderMap);
        if (member == null) {
            // 新用户,添加用户信息至用户表 t_member
            member = new Member();
            member.setName(name);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setSex(sex);
            // 根据身份证转生日
            Date birthday = stringToBirthday(idCard);
            member.setBirthday(birthday);
            // 添加member
            memberDao.addMember(member);

        } else {
            // map添加一项memberId
            orderMap.put("member_id",member.getId());
            // 确认用户是否预约过该套餐(同一天不能预约同一个套餐(id & orderDate & setmealId)
            List<Order> list = orderDao.isOrdered(orderMap);
            if (list.size() > 0) {
                // 预约过,不需要再次预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        // 以上条件都不满足,则添加预约
        orderMap.put("member_id",member.getId());

        // 用来接收返回的id
        orderMap.put("id",null);
        orderDao.addOrder(orderMap);
        //10.更新预约设置表 预约人数+1
        orderSettingDao.editReservationsByOrderDate(newOrderDate);
        // order对象返回 页面需要用体检预约id 关联获取体检预约信息
        return new Result(true,MessageConstant.ORDER_SUCCESS,orderMap);

    }

    /**
     * 根据orderId查找orderInfo
     * @param id
     * @return
     */
    @Override
    public Map getOrderInfoByOrderId(Integer id) throws Exception {
        // 根据orderId查找order信息
        Map map = orderDao.findByOrderId(id);
        Date orderDate = (Date)map.get("orderDate");
        String StringOrderDate = DateUtils.parseDate2String(orderDate);
        map.put("orderDate",StringOrderDate);
        return map;
    }



    /**
     * id转生日
     * @param id
     * @return
     * @throws ParseException
     */
    public Date stringToBirthday(String id) throws ParseException {
        String substring = id.substring(6, 14);
        String year = substring.substring(0, 4);
        String month = substring.substring(4, 6);
        String day = substring.substring(6);
        String birthday = year + "-" + month + "-" + day;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.parse(birthday);
    }


}
